﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using Microsoft.Expression.Interactivity.Layout;
using StrategyMap.BusinessLayer;

namespace StrategyMap.UserControls
{
    public partial class Node1 : UserControl
    {
        public event MouseButtonEventHandler StartDrawingArrowEvent;
        public event MouseButtonEventHandler EventShowNodeDetails;

        public bool NodeTargetEntered;

        public Node1()
        {
            InitializeComponent();
            AddDragBehavior();
        }

        
        public double nWidth
        {
            get
            {
                return 160;
                //return Main.Width + 2 * Left.Width;
            }
            //set;
        }

        public double nHeight
        {
            get
            {
                return 80;
                // return Main.Height + 2 * Upper.Height;
            }
            //set;
        }

        public int Radius
        {
            get
            {
                return (int)Main.RadiusX;
            }
            set
            {
                Main.RadiusX = value;
                Main.RadiusY = value;
            }
        }

        private void AddDragBehavior()
        {
            MouseDragElementBehavior drag = new MouseDragElementBehavior();
            drag.ConstrainToParentBounds = true;

            Interaction.GetBehaviors(this).Add(drag);
        }

        public void AttachDragBehavior()
        {
            foreach (var behavior in Interaction.GetBehaviors(this))
            {
                if (behavior.GetType() == typeof(MouseDragElementBehavior))
                {
                    behavior.Attach(this);
                }
            }
        }

        private void DetachDragBehavior()
        {
            foreach (var behavior in Interaction.GetBehaviors(this))
            {
                if (behavior.GetType() == typeof(MouseDragElementBehavior))
                {
                    behavior.Detach();
                }
            }
        }

        private void NodeLayoutRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            Left.Visibility = System.Windows.Visibility.Visible;
            Right.Visibility = System.Windows.Visibility.Visible;
            Upper.Visibility = System.Windows.Visibility.Visible;
            Lower.Visibility = System.Windows.Visibility.Visible;

            Main.StrokeThickness = 3;
            this.NodeLayoutRoot.Cursor = Cursors.Hand;
        }

        private void NodeLayoutRoot_MouseLeave(object sender, MouseEventArgs e)
        {
            HideRectagles();

            Main.StrokeThickness = 1;
            this.NodeLayoutRoot.Cursor = Cursors.Arrow;
        }

        private void NodeLayoutRoot_MouseLeave_Clicked(object sender, MouseEventArgs e)
        {
            HideRectagles();
            this.NodeLayoutRoot.Cursor = Cursors.Arrow;
            //Main.StrokeThickness = 1;
        }

        public void ChangeMouseLeave_Click()
        {
            this.NodeLayoutRoot.MouseLeave -= NodeLayoutRoot_MouseLeave;
            this.NodeLayoutRoot.MouseLeave += NodeLayoutRoot_MouseLeave_Clicked;
        }

        public void ChangeMouseLeave_UnClick()
        {
            Main.StrokeThickness = 1;
            this.NodeLayoutRoot.MouseLeave += NodeLayoutRoot_MouseLeave;
            this.NodeLayoutRoot.MouseLeave -= NodeLayoutRoot_MouseLeave_Clicked;
        }

        //public void AddMouseLeave()
        //{
        //    this.NodeLayoutRoot.MouseLeave += NodeLayoutRoot_MouseLeave;
        //}
        //public void RemoveMouseLeave()
        //{
        //    this.NodeLayoutRoot.MouseLeave -= NodeLayoutRoot_MouseLeave;
        //}

        public void HideRectagles()
        {
            Left.Visibility = System.Windows.Visibility.Collapsed;
            Right.Visibility = System.Windows.Visibility.Collapsed;
            Upper.Visibility = System.Windows.Visibility.Collapsed;
            Lower.Visibility = System.Windows.Visibility.Collapsed;

        }

        //public void NodeLayoutRoot_MouseLeave_KeepStroke()
        //{
        //    HideRectagles();
        //    Main.StrokeThickness = 2;
        //}

        public void NodeLayoutRoot_MouseLeave_CancelStroke()
        {
            Main.StrokeThickness = 1;
        }

        private void ConnectionPoint_MouseEnter(object sender, MouseEventArgs e)
        {
            ((Rectangle)sender).Fill = new SolidColorBrush(Colors.Green);
        }

        private void ConnectionPoint_MouseLeave(object sender, MouseEventArgs e)
        {
            ((Rectangle)sender).Fill = new SolidColorBrush(ColorTranslator.TranslateFromHex("#FF89E7F3"));
        }

        private void ConnectionPoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ((Rectangle)sender).Fill = new SolidColorBrush(Colors.Blue);
            //Interaction.GetBehaviors(this).RemoveAt(0);
            DetachDragBehavior();
            
            if (StartDrawingArrowEvent != null)
            {
                StartDrawingArrowEvent(this, e);
            }
        }

        private void ConnectionPoint_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            // AttachDragBehavior();
        }

        ////public event MouseButtonEventHandler StopDrawingArrowEvent;
        private void NodeLayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //if (StopDrawingArrowEvent != null)
            //{
            //    StopDrawingArrowEvent(this, e);
            //}
        }

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //if (EventShowNodeDetails != null)
            //{
            //    EventShowNodeDetails(sender, e);
            //}
        }


        //private void Main_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    if (EventShowNodeDetails != null)
        //    {
        //        EventShowNodeDetails(sender, e);
        //    }
        //}

        private void NodeLayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //if (EventShowNodeDetails != null)
            //{
            //    EventShowNodeDetails(sender, e);
            //}
        }

        //private void DrawingStopped()
        //{
        //    this.MouseEnter -= Node_MouseEnter;
        //    this.MouseLeave -= Node_MouseLeave;
        //    this.MouseLeftButtonUp -= Node_MouseLeftButtonUp;
        //}

    }

    
}
